package com.propic.www;

import com.leapmotion.leap.Controller;
import com.leapmotion.leap.Listener;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Tooltip;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;


import java.io.File;

/**
 * Created by geange on 16-5-15.
 */
public class Showpane {
    Listener listener;
    Controller c;

    public static boolean hasbutton = true;
    private static int flag = 0;
    public static boolean showEnd = false;
    public static int showEndnum = 0;
    public static int border = 30;
    public static boolean stop = false;
    public static int count = 0;
    public Stage showpic(File f){
        Stage stage = new Stage();
        //目录下图片的总数
        int sum = Mainwindows.files.size();
        //当前图片的索引
        int fileindex = Mainwindows.files.indexOf(f);
        flag = fileindex;
        BorderPane borderPane = new BorderPane();
        StackPane stackPane = new StackPane();
        FlowPane stackPaneguide = new FlowPane();

        Button button = new Button();
        Button leftbutton = new Button();
        Button rightbutton = new Button();

        //设置按钮的形状
        button.setStyle(
                "-fx-background-radius: 5em; " +
                        "-fx-min-width: 40px; " +
                        "-fx-min-height: 40px; " +
                        "-fx-max-width: 40px; " +
                        "-fx-max-height: 40px;"
        );

        leftbutton.setStyle(
                "-fx-background-radius: 5em; " +
                        "-fx-min-width: 40px; " +
                        "-fx-min-height: 40px; " +
                        "-fx-max-width: 40px; " +
                        "-fx-max-height: 40px;"
        );

        rightbutton.setStyle(
                "-fx-background-radius: 5em; " +
                        "-fx-min-width: 40px; " +
                        "-fx-min-height: 40px; " +
                        "-fx-max-width: 40px; " +
                        "-fx-max-height: 40px;"
        );

        c = new Controller();
        listener = new showpanelistener(this);
        c.addListener(listener);


        Image image = new Image("file:"+f.getAbsolutePath());
        ImageView imageView = new ImageView(image);

        GridPane gridpane = new GridPane();
        gridpane.setAlignment(Pos.CENTER);
        gridpane.setPadding(new Insets(0, 50, 50, 0));
        gridpane.setHgap(60);
        gridpane.setVgap(0);

        gridpane.add(leftbutton, 0, 0);
        gridpane.add(button, 1, 0);
        gridpane.add(rightbutton,2,0);
//        GridPane.setHalignment();
        stackPane.getChildren().add(imageView);
//        stackPane.setStyle("-fx-background-color:#3a4877");

        borderPane.setCenter(stackPane);
        borderPane.setBottom(gridpane);

        stackPane.setMaxSize(1000, 650);
        stackPane.setMinSize(1000, 650);

        //按钮的图像
        Image rimg = new Image(String.valueOf(getClass().getResource("res/rightwhite.png")));
        Image limg = new Image(String.valueOf(getClass().getResource("res/leftwhite.png")));
        Image showimg = new Image(String.valueOf(getClass().getResource("res/show.png")));      //开始播放图片
        Image pauseimg = new Image(String.valueOf(getClass().getResource("res/pause.png")));    //停止播放图片
        ImageView rimgV = new ImageView(rimg);
        ImageView simgV = new ImageView(showimg);
		ImageView limgV = new ImageView(limg);
        ImageView pimgV = new ImageView(pauseimg);
        //设置按钮图像
        leftbutton.setGraphic(limgV);
        button.setGraphic(simgV);
        rightbutton.setGraphic(rimgV);

        //舍子显示图片初始大小
        int borderPictmp = 30;
        if (image.getHeight()/image.getWidth()>0.62) {
            imageView.setFitHeight(650-borderPictmp);
            imageView.setFitWidth(((650-borderPictmp)/image.getHeight())*image.getWidth());
        }else{
            imageView.setFitHeight(((1000-borderPictmp)/image.getWidth())*image.getHeight());
            imageView.setFitWidth(1000-borderPictmp);
        }


        button.setOnAction(e->{
            if (stop){
                stage.setFullScreen(false);
                stackPane.getChildren().clear();
                stackPane.getChildren().add(imageView);
                button.setGraphic(simgV);
                stop = false;
            }else {
                gridpane.getChildren().clear();
                hasbutton = false;
                stage.setFullScreen(true);
                button.setGraphic(pimgV);
                SimpleSlideShow simpleSlideShow = new SimpleSlideShow();
                String AimnameforShow = f.getParent();
                simpleSlideShow.setAll(AimnameforShow);
                StackPane tmppane = simpleSlideShow.getRoot();
                stackPane.getChildren().clear();
                stackPane.getChildren().add(tmppane);
                simpleSlideShow.start();
                stackPane.setOnMouseClicked(t->{
                    if (count==1 && hasbutton){
                        gridpane.getChildren().clear();
                        hasbutton = false;
                        count = 0;
//                        System.out.println("再次点击按钮消失");
                    }else if (!hasbutton){
                        gridpane.add(leftbutton, 0, 0);
                        gridpane.add(button, 1, 0);
                        gridpane.add(rightbutton,2,0);
                        hasbutton = true;
                        count = 1;
                    }

                });
                stop = true;
            }

        });

        leftbutton.setOnAction(e->{
            if (flag>0){
                System.out.println("前一幅");
                File tmp = Mainwindows.files.get(flag-1);
                Image imagetmp = new Image("file:"+tmp.getAbsolutePath());
                ImageView imageViewtmp = new ImageView(imagetmp);
                System.out.println(flag-1);

                if (imagetmp.getHeight()/imagetmp.getWidth() > 0.62){
                    imageViewtmp.setFitHeight(650 - border);
                    imageViewtmp.setFitWidth((650-border)/imagetmp.getHeight()*imagetmp.getWidth());
                }else {
                    imageViewtmp.setFitHeight((1000-border)/imagetmp.getWidth()*imagetmp.getHeight());
                    imageViewtmp.setFitWidth(1000-border);
                }
                stackPane.getChildren().clear();
                imageViewtmp.setOnScroll(wheel-> {
                    double m = wheel.getDeltaY();
                    System.out.println(m);
                    ShowFolderPic.flagold = ShowFolderPic.flag;
                    if (m > 0) {
                        ShowFolderPic.flag = true;
                    } else {
                        ShowFolderPic.flag = false;
                    }
                    double tmpimg = imageViewtmp.getFitHeight();
                    if (imageViewtmp.getFitHeight() > 300 && imageViewtmp.getFitHeight() < 1000 && imageViewtmp.getFitWidth() > 500 && imageViewtmp.getFitWidth() < 1920) {
                        imageViewtmp.setFitHeight(imageViewtmp.getFitHeight() + m * 0.3);
                        imageViewtmp.setFitWidth(imageViewtmp.getFitWidth() / tmpimg * imageViewtmp.getFitHeight());
                    } else if (ShowFolderPic.flagold != ShowFolderPic.flag) {
                        imageViewtmp.setFitHeight(imageViewtmp.getFitHeight() + m * 0.3);
                        imageViewtmp.setFitWidth(imageViewtmp.getFitWidth() / tmpimg * imageViewtmp.getFitHeight());
                    }
                });
                stackPane.getChildren().add(imageViewtmp);

                flag = flag - 1;

            }else {
                showEnd = true;
                if (showEnd & showEndnum == 0){
                    Stage stagetmp = new ShowEndpane().showendpaneleft();
                    stagetmp.show();
                    showEndnum = 1;
                    stagetmp.setAlwaysOnTop(true);


                }

            }

        });

        rightbutton.setOnAction(e->{
            if (flag<sum-1){
                System.out.println("后一幅");
                File tmp = Mainwindows.files.get(flag+1);
                Image imagetmp = new Image("file:"+tmp.getAbsolutePath());
                ImageView imageViewtmp = new ImageView(imagetmp);
                System.out.println(flag+1);

                if (imagetmp.getHeight()/imagetmp.getWidth() > 0.62){
                    imageViewtmp.setFitHeight(650 - border);
                    imageViewtmp.setFitWidth((650-border)/imagetmp.getHeight()*imagetmp.getWidth());
                }else {
                    imageViewtmp.setFitHeight((1000-border)/imagetmp.getWidth()*imagetmp.getHeight());
                    imageViewtmp.setFitWidth(1000-border);
                }
                stackPane.getChildren().clear();
                imageViewtmp.setOnScroll(wheel-> {
                    double m = wheel.getDeltaY();
                    System.out.println(m);
                    ShowFolderPic.flagold = ShowFolderPic.flag;
                    if (m > 0) {
                        ShowFolderPic.flag = true;
                    } else {
                        ShowFolderPic.flag = false;
                    }
                    double tmpimg = imageViewtmp.getFitHeight();
//                    imageView.setFitHeight(imageViewtmp.getFitHeight() + m * 0.3);
                    if (imageViewtmp.getFitHeight() > 300 && imageViewtmp.getFitHeight() < 1000 && imageViewtmp.getFitWidth() > 500 && imageViewtmp.getFitWidth() < 1920) {
                        imageViewtmp.setFitHeight(imageViewtmp.getFitHeight() + m * 0.3);
                        imageViewtmp.setFitWidth(imageViewtmp.getFitWidth() / tmpimg * imageViewtmp.getFitHeight());
                    } else if (ShowFolderPic.flagold != ShowFolderPic.flag) {
                        imageViewtmp.setFitHeight(imageViewtmp.getFitHeight() + m * 0.3);
                        imageViewtmp.setFitWidth(imageViewtmp.getFitWidth() / tmpimg * imageViewtmp.getFitHeight());
                    }
                });
                stackPane.getChildren().add(imageViewtmp);

                flag = flag + 1;

            }else {
                showEnd = true;
                if (showEnd & showEndnum == 0){
                    Stage stagetmp = new ShowEndpane().showendpaneright();
                    stagetmp.show();
                    showEndnum = 1;
                    stagetmp.setAlwaysOnTop(true);


                }

            }

        });


        imageView.setOnScroll(wheel-> {
            double m = wheel.getDeltaY();
            System.out.println(m);
            ShowFolderPic.flagold = ShowFolderPic.flag;
            if (m > 0) {
                ShowFolderPic.flag = true;
            } else {
                ShowFolderPic.flag = false;
            }
            double tmp = imageView.getFitHeight();
            if (imageView.getFitHeight() > 300 && imageView.getFitHeight() < 1000 && imageView.getFitWidth() > 500 && imageView.getFitWidth() < 1920) {
                imageView.setFitHeight(imageView.getFitHeight() + m * 0.3);
                imageView.setFitWidth(imageView.getFitWidth() / tmp * imageView.getFitHeight());
            } else if (ShowFolderPic.flagold != ShowFolderPic.flag) {
                imageView.setFitHeight(imageView.getFitHeight() + m * 0.3);
                imageView.setFitWidth(imageView.getFitWidth() / tmp * imageView.getFitHeight());
            }
        });



        Scene scene = new Scene(borderPane, 1000, 700);
        Tooltip tooltip = new Tooltip();
        tooltip.setText("从头开始播放图片");
        button.setOnMouseEntered(in->{
            button.setTooltip(tooltip);
        });

        stage.setScene(scene);
        return stage;
    }
}
